/**
 * 部门列表
 * 文件命名格式：zmn.模块名称.表名称.功能.jas
 * @author heguanghua
 * @since 2018/10/18
 */
layui.define(['index', 'form', 'table', 'laypage', 'layedit', 'laydate'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var table = layui.table;
    var laypage = layui.laypage;

    var cols = [
        [
            {field: 'deptId', type: 'checkbox', fixed: 'left', width: 50, align: 'center'},
            {field: 'sort', title: '排序号', width: 130,fixed: 'left',  align: 'center', edit: json.hasEditSort ? 'text' : false, unresize: true, sort: false},
            {field: 'deptId', title: '部门ID', width: 175, fixed: 'left', align: 'center', unresize: true, sort: false},
            {field: 'deptName', title: '名称', width: 175, fixed: 'left', align: 'center', unresize: true, sort: false},
            {field: 'companyId', title: '公司ID', width: 175, align: 'center', unresize: true, sort: false, templet:(d)=>{
                    if (d.companyId) {
                        return d.companyId;
                    } else {
                        return "--";
                    }
                }},
            {field: 'companyName', title: '公司名称', width: 175, align: 'center', unresize: true, sort: false, templet:(d)=>{
                    if (d.companyName) {
                        return d.companyName;
                    } else {
                        return "--";
                    }
                }},
            {field: 'orgId', title: '组织ID', width: 175, align: 'center', unresize: true, sort: false},
            {field: 'orgName', title: '组织名称', width: 175, align: 'center', unresize: true, sort: false},
            {field: 'keyStaffs', title: '部门负责人', width: 175, align: 'center', unresize: true, sort: false, templet:(d)=>{
                if (d.keyStaffs) {
                    return d.keyStaffs;
                } else {
                    return "--";
                }
                }},
            {
                field: 'status', title: '状态', width: 175, align: 'center', unresize: true, templet: function (d) {
                    return zmnEnum.STATUS[d.status];
                }
            },
            {field: 'creater', title: '创建人', width: 175, align: 'center', unresize: true},
            {field: 'createTime', title: '创建时间', templet: '#createTime', width: 200, align: 'center', unresize: true,
                templet :function (row){
                    return zmnUtil.createTime(row.createTime);
                }
            }, {field: 'updater', title: '编辑人', width: 175, align: 'center', unresize: true, templet:(d)=>{
                if (d.updater) {
                    return d.updater;
                } else {
                    return "--";
                }
            }},
            {field: 'updateTime', title: '编辑时间', width: 175, align: 'center', unresize: false,
                templet :function (row){
                    return zmnUtil.createTime(row.updateTime);
                }
            }, {
                fixed: 'right', title: '操作', width: 205, align: 'center', templet:
                    '<div>' +
                    (json.hasView ? '<a href="/mc/dept/list.action?parentId={{d.deptId}}" class="layui-btn layui-btn-xs">下级部门</a>' : '') +
                    (json.hasEdit ? '<a lay-href="/mc/dept/detail.action?deptId={{d.deptId}}" class="layui-btn layui-btn-xs">编辑</a>' : '') +
                    (json.hasPermitList ? '<a lay-href="/mc/permit/data/list.action?deptId={{d.deptId}}" class="layui-btn layui-btn-xs">数据权限</a>' : '') +
                    '</div>'
            }
        ]
    ];
    table.render({
        elem: "#zmn-mc-dept-table",
        toolbar: "#zmn-mc-dept-table-tool",
        title: "分类列表",
        data: json.items,
        limit: json.pageSize,
        cols: cols,
        page: true,
        height: 'full-120'
    });

    laypage.render(zmnPage.laypageOptions(json));

    var ids = new Array();

    // 选择（全选或单选）
    table.on("checkbox(zmn-mc-dept-table)", function () {
        ids = zmnPage.layIdsChecked(json.items, "deptId");
    });

    // 启用/停用
    table.on("toolbar(zmn-mc-dept-table)", function (obj) {
        if (obj.event != "start" && obj.event != "stop") {
            return;
        }
        if (ids.length === 0) {
            return layer.alert("请选择至少一个部门！");
        }
        zmnPage.layStartStop(obj, "/mc/dept/updateStatus.action", ids.join(","));
    });

    // 修改排序
    table.on("edit(zmn-mc-dept-table)", function (obj) {
        zmnPage.laySort(obj, "/mc/dept//updateSort.action", obj.data.deptId);
    });

    //对外暴露的接口
    exports('art_dept_list', {});
});